﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using WgFrame.Security;
using WgFrame.Net;
using FairyGUI;

public class TestMain : MonoBehaviour, IConnectionCallbacks
{
    private const int ManualWidth = 640;
    private const int ManualHeight = 1136;

    public virtual void OnEnable()
    {
        GameNetwork.AddCallbackTarget(this);
    }

    public virtual void OnDisable()
    {
        GameNetwork.RemoveCallbackTarget(this);
    }

    void Start()
    {
        GRoot.inst.SetContentScaleFactor(ManualWidth, ManualHeight, UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);
        UIConfig.defaultFont = "Alibaba-PuHuiTi-Regular";
        LoadStaticData();

        PanelManager.Open<LoginPanel>();
    }

    public void OnDestroy()
    {
        GameNetwork.Disconnect();
    }

    public void OnApplicationQuit()
    {
        GameNetwork.Disconnect();
    }

    void Update()
    {
    }

    private void LateUpdate()
    {
        GameNetwork.Service();
    }

    public void OnConnected()
    {
        Debug.Log("连接成功");
        GameNetwork.IsConnected = true;
        RequestHelper.SendStartRequest();
    }

    public void OnDisconnected()
    {
        Debug.Log("断开连接");
        if (GameNetwork.IsConnected)
        {
            PopupPanelManager.Open<DiscountPanel>();
        }
        GameNetwork.IsConnected = false;
    }

    public void OpResponseReceived(OperationResponse response)
    {
        if (response.ReturnCode != 0)
        {
            FlyTextPanelMgr.Show(response.DebugMessage);
            Debug.LogError("response error: code=" + response.OperationCode + " returncode=" + response.ReturnCode + " msg=" + response.DebugMessage);
            return;
        }

        switch ((OperationCode)response.OperationCode)
        {
            case OperationCode.GameStart:
                {
                    ResponseHelper.HandleGameStartResponse(response.Parameters);
                    break;
                }
            case OperationCode.CreatePlayer:
                {
                    ResponseHelper.HandleCreatePlayerResponse(response.Parameters);
                    break;
                }
            case OperationCode.PlayerInfo:
                {
                    ResponseHelper.HandleGetPlayerResponse(response.Parameters);
                    break;
                }
            case OperationCode.ItemInfo:
                {
                    ResponseHelper.HandleGetItemResponse(response.Parameters);
                    break;
                }
            case OperationCode.ShangZhen:
                {
                    ResponseHelper.HandleShangZhenResponse(response.Parameters);
                    break;
                }
            case OperationCode.BuZhen:
                {
                    ResponseHelper.HandleBuZhenResponse(response.Parameters);
                    break;
                }
            case OperationCode.DownEquip:
                {
                    ResponseHelper.HandleDownEquipResponse(response.Parameters);
                    break;
                }
            case OperationCode.WearEquip:
                {
                    ResponseHelper.HandleWearEquipResponse(response.Parameters);
                    break;
                }
            case OperationCode.Chat:
                {
                    ResponseHelper.HandleChatResponse(response.Parameters);
                    break;
                }
            case OperationCode.QiangHuaEquip:
                {
                    ResponseHelper.HandleQiangHuaEquipResponse(response.Parameters);
                    break;
                }
            case OperationCode.JingLianEquip:
                {
                    ResponseHelper.HandleJingLianEquipResponse(response.Parameters);
                    break;
                }
            case OperationCode.ZhuLingEquip:
                {
                    ResponseHelper.HandleZhuLinEquipResponse(response.Parameters);
                    break;
                }
            case OperationCode.LingShiQiangHua:
                {
                    ResponseHelper.HandleLinShiQiangHuaResponse(response.Parameters);
                    break;
                }
            case OperationCode.BuyShop:
                {
                    ResponseHelper.HandleBuyShopResponse(response.Parameters);
                    break;
                }
            case OperationCode.ZhaoMu:
                {
                    ResponseHelper.HandleZhaoMuResponse(response.Parameters);
                    break;
                }
        }
    }

    private void LoadStaticData()
    {
        InitHeroMgr.Load();
        InitEquipMgr.Load();
        RoleUpMgr.Load();
        InitSkillMgr.Load();
        EquipJianLianUpMgr.Load();
        ZhuLingClothesMgr.Load();
        ZhuLingWeaponMgr.Load();
        ZhuLingRingMgr.Load();
        ZhuLingShoeMgr.Load();
        EquipJianLianMgr.Load();
        EquipQiangHuaMgr.Load();
        EquipLingShiMgr.Load();
        InitItemMgr.Load();
        MonsterMgr.Load();
        InitShopMgr.Load();
        RecruitHeroMgr.Load();
        InitDataMgr.Load();
    }

}